/*     */ package com.services.bo;
/*     */ 
/*     */ import java.io.PrintStream;
/*     */ import java.math.BigDecimal;
/*     */ import java.security.InvalidKeyException;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import javax.crypto.BadPaddingException;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.IllegalBlockSizeException;
/*     */ import javax.crypto.NoSuchPaddingException;
/*     */ import javax.crypto.SecretKey;
/*     */ import javax.crypto.SecretKeyFactory;
/*     */ import javax.crypto.spec.DESedeKeySpec;
/*     */ 
/*     */ public class GenerateAuthKey
/*     */ {
/*     */   private SecretKey desKey;
/*     */   private static final String HEX_DIGITS = "0123456789ABCDEF";
/*     */ 
/*     */   private void createCipher(byte[] desKeyData)
/*     */     throws Exception
/*     */   {
/*  22 */     if ((desKeyData.length != 16) && (desKeyData.length != 24)) {
/*  23 */       throw new Exception("Length not valid :" + desKeyData.length);
/*     */     }
/*  25 */     byte[] key = new byte[24];
/*  26 */     if (desKeyData.length == 16) {
/*  27 */       for (int za = 0; za < 16; za++) {
/*  28 */         key[za] = desKeyData[za];
/*     */       }
/*  30 */       for (int za = 0; za < 8; za++) {
/*  31 */         key[(za + 16)] = desKeyData[za];
/*     */       }
/*     */     }
/*  34 */     if (desKeyData.length == 24) {
/*  35 */       for (int za = 0; za < 24; za++) {
/*  36 */         key[za] = desKeyData[za];
/*     */       }
/*     */     }
/*  39 */     DESedeKeySpec desKeySpec = new DESedeKeySpec(key);
/*  40 */     SecretKeyFactory keyFactory = null;
/*  41 */     keyFactory = SecretKeyFactory.getInstance("DESede");
/*  42 */     this.desKey = keyFactory.generateSecret(desKeySpec);
/*     */   }
/*     */ 
/*     */   private byte[] encryptECB(byte[] cleartext) {
/*  46 */     byte[] ciphertext = null;
/*     */     try
/*     */     {
/*  49 */       Cipher desCipher = Cipher.getInstance("DESede/ECB/NoPadding");
/*  50 */       desCipher.init(1, this.desKey);
/*  51 */       ciphertext = desCipher.doFinal(cleartext);
/*     */     } catch (NoSuchAlgorithmException ex1) {
/*     */     } catch (InvalidKeyException ex2) {
/*     */     } catch (NoSuchPaddingException ex3) {
/*     */     } catch (BadPaddingException ex4) {
/*     */     } catch (IllegalBlockSizeException ex5) {
/*     */     } catch (IllegalStateException ex6) {
/*     */     }
/*  59 */     return ciphertext;
/*     */   }
/*     */ 
/*     */   public String generateAuthKey(String data, String keyId) throws Exception {
/*  63 */     MessageDigest sha = MessageDigest.getInstance("MD5");
/*  64 */     sha.update(data.toString().getBytes());
/*  65 */     byte[] byteHash = sha.digest();
/*  66 */     String hashKey = toHexString(byteHash);
/*     */ 
/*  68 */     byte[] byteHashKey = fromHexString(hashKey);
/*  69 */     byte[] byteKey = fromHexString(keyId);
/*  70 */     createCipher(byteKey);
/*  71 */     byte[] byteEncryptedHashKey = encryptECB(byteHashKey);
/*  72 */     String encryptedHashKey = toHexString(byteEncryptedHashKey);
/*  73 */     return encryptedHashKey;
/*     */   }
/*     */ 
/*     */   private byte[] fromHexString(String s)
/*     */   {
/*  78 */     byte[] bytes = new byte[s.length() / 2];
/*  79 */     for (int i = 0; i < s.length() / 2; i++) {
/*  80 */       bytes[i] = (byte)Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
/*     */     }
/*  82 */     return bytes;
/*     */   }
/*     */ 
/*     */   public String toHexString(byte[] b) {
/*  86 */     if ((b == null) || (b.length == 0)) {
/*  87 */       return "";
/*     */     }
/*  89 */     return toHexString(b, 0, b.length);
/*     */   }
/*     */ 
/*     */   public String toHexString(byte[] b, int off, int len)
/*     */   {
/*  95 */     StringBuffer s = new StringBuffer();
/*  96 */     for (int i = off; i < off + len; i++) {
/*  97 */       s.append("0123456789ABCDEF".charAt((b[i] & 0xFF) >> 4));
/*  98 */       s.append("0123456789ABCDEF".charAt(b[i] & 0xF));
/*     */     }
/* 100 */     return s.toString();
/*     */   }
/*     */ 
/*     */   private int getHash(String val) {
/* 104 */     int h = 0;
/* 105 */     char[] vals = val.toCharArray();
/* 106 */     for (int i = 0; i < vals.length; i++) {
/* 107 */       h = h * 31 + vals[i];
/*     */     }
/* 109 */     return h;
/*     */   }
/*     */ 
/*     */   public String generateSignature(String klikPayCode, String transactionDate, String transactionNo, String amount, String currency, String keyId) throws Exception
/*     */   {
/* 114 */     Date dateTmp = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse(transactionDate);
/* 115 */     int transDate = Integer.parseInt(new SimpleDateFormat("ddMMyyyy").format(dateTmp));
/* 116 */     int amt = new Double(amount).intValue();
/*     */ 
/* 119 */     String tempKey1 = klikPayCode + transactionNo + currency + keyId;
/* 120 */     int hashKey1 = getHash(tempKey1);
/* 121 */     System.out.println("generateSignature tempKey1 : " + tempKey1 + ", hashKey1 : " + hashKey1);
/*     */ 
/* 124 */     String tempKey2 = Integer.toString(transDate + amt);
/* 125 */     int hashKey2 = getHash(tempKey2);
/* 126 */     System.out.println("generateSignature tempKey2 : " + tempKey2 + ", hashKey2 : " + hashKey2);
/*     */ 
/* 129 */     BigDecimal hash = new BigDecimal(hashKey1).add(new BigDecimal(hashKey2)).abs();
/* 130 */     System.out.println("generateSignature hashKey : " + hash);
/*     */ 
/* 132 */     return hash.toString();
/*     */   }
/*     */ 
/*     */   public static void main(String[] args) {
/* 136 */     GenerateAuthKey client = new GenerateAuthKey();
/* 137 */     String clearKey = "";
/* 138 */     String klikPayCode = "";
/* 139 */     String transactionNo = "";
/* 140 */     String transactionDate = "";
/* 141 */     String amount = "";
/* 142 */     String currency = "IDR";
/*     */ 
/* 144 */     String keyId = client.toHexString(clearKey.getBytes());
/* 145 */     System.out.println("keyId : " + keyId);
/*     */ 
/* 147 */     String data = String.format("%-10s", new Object[] { klikPayCode }).replace(' ', '0') + String.format("%-18s", new Object[] { transactionNo }).replace(' ', 'A') + String.format("%-5s", new Object[] { currency }).replace(' ', '1') + transactionDate + String.format("%-32s", new Object[] { keyId }).replace(' ', 'E');
/*     */     try
/*     */     {
/* 154 */       String signature = client.generateSignature(klikPayCode, transactionDate, transactionNo, amount, currency, keyId);
/* 155 */       System.out.println("signature : " + signature);
/*     */ 
/* 157 */       String authKey = client.generateAuthKey(data, keyId);
/* 158 */       System.out.println("authKey : " + authKey);
/*     */     } catch (Exception ex) {
/* 160 */       ex.printStackTrace();
/*     */     }
/*     */   }
/*     */ }

/* Location:           F:\BCA.jar
 * Qualified Name:     com.services.bo.GenerateAuthKey
 * JD-Core Version:    0.6.0
 */